<?php

class ImageProcessing
{
    public $folder = 'upload';
    public $file = '';
    public $thumbs = array();


    /**
     * @todo Create thumb images to specific folders from exist image 
     * @example 
     * $ImageProcessing = new ImageProcessing();
     * $ImageProcessing->folder = '/upload/admin/artist';         
     * $ImageProcessing->file = 'photo.jpg';
     * $ImageProcessing->thumbs =array('thumb1' => array('width'=>'1336','height'=>'768'),
     *                                  'thumb2' =>  array('width'=>'800','height'=>'600'));
     *   $ImageProcessing->create_thumbs();
     * @author bb
     */
    public function create_thumbs()
    {
       if(count($this->thumbs) > 0)
       {
           $this->createDirectoryByPath($this->folder);
           foreach($this->thumbs as $folderThumb => $size)
           {
                $this->createSingleDirectoryByPath($this->folder.'/'.$folderThumb);

                $thumb=new EPhpThumb($this->folder);
                $thumb->init();
                $thumb->create(Yii::getPathOfAlias('webroot').$this->folder.'/'.$this->file)
                    ->resize($size['width'],$size['height'])
                    ->save(Yii::getPathOfAlias('webroot').$this->folder.'/'.$folderThumb.'/'.$this->file);
           }
       }
    }

    /**
     * @todo delete image file
     * @param type $source: "/upload/admin/artist/photo.jpg"
     */
    public function delete($source)//  "/upload/admin/artist/photo.jpg"
    {
//        if(file_exists(Yii::getPathOfAlias("webroot").$source))
//            unlink(Yii::getPathOfAlias("webroot").$source);
        if(file_exists($source))
            unlink($source);
    }
    
    /** it is static function 
     * @todo delete image file
     * @param type $source: "/upload/admin/artist/photo.jpg"
     */
    public static function deleteFile($source)//  "/upload/admin/artist/photo.jpg"
    {
        if(file_exists(Yii::getPathOfAlias("webroot").$source))
            unlink(Yii::getPathOfAlias("webroot").$source);
    }	
	
    /**
     * 
     * @todo RESIZE & CROP
     * @param type $fileName
     * @example 
     * $ImageProcessing = new ImageProcessing();
          $ImageProcessing->folder = '/upload/admin/artist';
          $ImageProcessing->file = 'photo.jpg';
          $ImageProcessing->thumbs =array('width'=>'1336','height'=>'768');
          $ImageProcessing->resizeAndCrop('fileNameOfDestinationImage');
     * @author bb
     */
    public function resizeAndCrop($fileName)
    {
          $thumb=new EPhpThumb();
          $thumb->init();
          $thumb->create(Yii::getPathOfAlias('webroot').$this->folder.'/'.$this->file)
              ->adaptiveResize($this->thumbs['width'],$this->thumbs['height'])
              ->save(Yii::getPathOfAlias('webroot').$this->folder.'/'.$fileName);
    }
      
    /**
     * 
     * @todo create directory from path
     * @param type $path: '/upload/admin/artist'
     */
    public function createDirectoryByPath($path)
    {
        $aFolder = explode('/',$path);
        if(is_array($aFolder))
        {
            $this->removeEmptyItemFromArray($aFolder);
            $root = Yii::getPathOfAlias('webroot');

            $currentPath = $root;
            foreach($aFolder as $key =>$folder)
            {
                $currentPath =  $currentPath.'/'.$folder;
                if(!is_dir($currentPath))
                    mkdir($currentPath);
            }
        }
    }

    public function removeEmptyItemFromArray(&$arr)
    {
        foreach($arr as $key=>$value)
        if (is_null($value)) {
              unset($arr[$key]);
            }
    }

    
	/**
     * @todo create directory from path
     * @param type $path: /upload/member/555/avatar
     */
    public static function createDirectory($path)
    {
        $aFolder = explode('/',$path);
        if(is_array($aFolder))
        {
            $root = Yii::getPathOfAlias('webroot');
            $currentPath = $root;
            foreach($aFolder as $folder)
            {
                $currentPath =  $currentPath.'/'.$folder;
                if(!is_dir($currentPath)){
                    mkdir($currentPath);
                    chmod($currentPath, 0755);       
                }
            }
        }
    }	
	
    /**
     * 
     * @todo Create single directory. 
     *          Create directory from the last path.
     *          You have to make sure that the parent directorry already exists
     * @param string $path: '/upload/admin/artist/thumb'
     * 
     */
    public function createSingleDirectoryByPath($path)
    {
        $path = Yii::getPathOfAlias('webroot').$path;
        if(!is_dir($path))
                    mkdir($path);
    }

}
